//var xg=false;
var page=1;
var rows=10;
var jsonList={};
var isShow=false;
var zt_biao=[];
var  bxlist=[];
$('.tianjia').on('click',function(){
	$('#shouye').hide();
	$('#xz').show();
	fn()
	$(this).addClass('active')
	//xg=false
	//$.FreeTags('tags');
	$(".btn_xiugai").hide();
	$('#form-tags-4').tagsInput({
		'autocomplete': {
			source: zt_biao
		}
	});
})
//修改
$('.xiugai').on('click',function(){
	$('#xz').show();
	$(".btn_xiugai").show();
	$('.btn_sure').hide();
	// if(xg==false){
	// 	alert('添加以后不能直接修改');
	// 	$('#shouye').show();
	// 	$('#xz').hide()
	// 	fn()
	// 	return false
	// }
	//修改前先判断是否有选中的并且是一个
	var numCount=0;
	var ids ="";
	$(".xuanze").each(function (index,key){
		if($(this).prop("checked")==true){
			numCount++;
			ids = $(this).attr("oid");
		}
		
	})
	if (numCount==1){
		//进行下一步
		$('#shouye').hide();
		$('#xz').show();
		fn()
		$(this).addClass('active');
		$.ajax({
			url: baseNews +"/api/data/special/info",
			data:{
				id: ids
			},
			success:function (res){
				//console.log(res,7878)
				if(res.code==0){
					var result=res.data;
					console.log(result,00000);
					$('.zdid').val(ids);
					$(".t_name").val(result.title);
					$(".t_top").prop("checked");
					var tags="";
					for (var i = 0; i < result.tagArray.length; i++) {
						
						for (var j = 0; j < bxlist.length; j++) {
							if (result.tagArray[i] == bxlist[j].id) {
								tags = tags + bxlist[j].tag + ",";
							}
						}
						// if (bxlist[i] == cont) {
						// 	tags = tags + bxlist[i].id + ","
						// }
					}
					$(".add_biaoq").attr("value", tags);
					$('#form-tags-4').tagsInput({
						'autocomplete': {
							source: zt_biao
						}
					});

					if(result.top==true){
						$(".t_top").prop("checked",true);
					}
					$(".t_jianjie").val(result.synopsis);
					if (result.cover!=""){
						$(".img0").attr("src", result.cover);
						$(".img0").attr("fsrc", result.cover);
					}
					if (result.img1 != "") {
						$(".img1").attr("src", result.img1);
						$(".img1").attr("fsrc", result.img1);
					}
					if (result.img2 != "") {
						$(".img2").attr("src", result.img2);
						$(".img2").attr("fsrc", result.img2);
					}
					if (result.img3 != "") {
						$(".img3").attr("src", result.img3);
						$(".img3").attr("fsrc", result.img3);
					}
					$(".b_bianqian").attr("value",result.tags);					
					$(".rd_m").val(result.inithot);
					//频道的默认选中
					var  checkOid=(result.cid);
					var sp=$(".ad_select").children("option");
					 $(sp).each(function(index,key){
						 if ($(this).attr("value") == checkOid){
							$(this).attr("selected",'selected')
						}
					 })
					 //是否展现大图
					var checkShow= (result.showtype);
					var sp = $(".app_f").children("option");
					 $(sp).each(function(index,key){
						 if ($(this).attr("value") == checkShow){
							$(this).attr("selected",'selected')
						}
					 })

					 var  checkOid=(result.cid);
					var sp=$(".ad_select").children("option");
					 $(sp).each(function(index,key){
						 if ($(this).attr("value") == checkOid){
							$(this).attr("selected",'selected')
						}
					 })
					//$.FreeTags('tags');
				}
			}
		})
	}else{
		//遮罩层提示
		zeroModal.alert('请选择一条专题进行修改');

	}
	//return  false;
	
	
})
// 删除
$(".btn_sq").on("click",function (){
	var numCount = 0;
	var ids='';
	$(".xuanze").each(function (index, key) {
		if ($(this).prop("checked") == true) {
			numCount++;
			ids = ids + $(this).attr("oid") + ',';
		}
	})
	console.log(numCount, 555);
	if (numCount >= 1) {
		//进行下一步
		var json = {}
		var result = JSON.stringify(json);
			zeroModal.confirm("确定要删除该专题吗？", function () {
				//删除确认请求数据
				$.ajax({
					url: baseNews + "/api/data/special/del?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success:function (res){
						if(res.code==0){
							location.reload();
							
						}else{
							zeroModal.alert('删除失败');

						}
					}

				})
			});
	} else {
		//遮罩层提示
		zeroModal.alert('请选择一条专题进行删除');

	}
	//return false;
})
//置顶专题
$(".btn_zd").on('click',function (){
	var numCount = 0;
	var ids ='';
	$(".xuanze").each(function (index, key) {
		if ($(this).prop("checked") == true) {
			numCount++;
			ids = ids + $(this).attr("oid")+',';
		}
	})
	console.log(numCount, 555);
	if (numCount >= 1) {
		//进行下一步
		var json = {}
		var result = JSON.stringify(json);
		zeroModal.confirm("确定要置顶该专题吗？", function () {
			//置顶确认请求数据
			$.ajax({
				url: baseNews + "/api/data/special/top/true?ids=" + ids,
				type: "post",
				headers: { 'Content-Type': 'application/json' },
				dataType: "json",
				data: result,
				success: function (res) {
					if (res.code == 0) {
						location.reload();

					} else {
						zeroModal.alert('置顶失败');

					}
				}

			})
		});
	} else {
		//遮罩层提示
		zeroModal.alert('请选择至少一条专题进行置顶');

	}
	//return false;
})
//取消置顶
$(".btn_zdqx").on("click",function (){
	var numCount = 0;
	var ids = '';
	$(".xuanze").each(function (index, key) {
		if ($(this).prop("checked") == true) {
			numCount++;
			ids = ids + $(this).attr("oid") + ',';
		}
	})
	console.log(numCount, 555);
	if (numCount >= 1) {
		//进行下一步
		var json = {}
		var result = JSON.stringify(json);
		zeroModal.confirm("确定要取消置顶该专题吗？", function () {
			//置顶确认请求数据
			$.ajax({
				url: baseNews + "/api/data/special/top/false?ids=" + ids,
				type: "post",
				headers: { 'Content-Type': 'application/json' },
				dataType: "json",
				data: result,
				success: function (res) {
					if (res.code == 0) {
						location.reload();

					} else {
						zeroModal.alert('取消置顶失败');

					}
				}

			})
		});
	} else {
		//遮罩层提示
		zeroModal.alert('请选择至少1条专题取消置顶');

	}
	//return false;
	
})
$('.quxiao').on('click',function(){
	$('#shouye').show();
	fn()
	$('#xz').hide()
})
//移除class
function fn(){
		$('.b-t .b-t-s').removeClass('active')
}
$('.file').on('change',function(e){
	console.log($(this).index)
	var e=e||event;
	 var reader = new FileReader();
	 console.log(reader)
	reader.readAsDataURL($('.file')[0].files[0]);//发起异步请
	//上传图片1
	sendImg('.tu img', $('.file')[0].files[0])
	reader.onload = function(){
    var src=this.result
	$('.tu img').attr('src',src)
  }
})
$('.file1').on('change',function(e){
	console.log($(this).index)
	var e=e||event;
	 var reader = new FileReader();
	 console.log(reader)
	reader.readAsDataURL($('.file1')[0].files[0]);//发起异步请求
	//上传图片2
	sendImg('.tu1 img', $('.file1')[0].files[0])
	reader.onload = function(){
    var src=this.result
	$('.tu1 img').attr('src',src)
  }
})
$('.file2').on('change',function(e){
	console.log($(this).index)
	var e=e||event;
	var reader = new FileReader();
	console.log(reader)
	reader.readAsDataURL($('.file2')[0].files[0]);//发起异步请求
	//上传图片3
	sendImg('.tu2 img', $('.file2')[0].files[0])
	reader.onload = function(){
    var src=this.result
	$('.tu2 img').attr('src',src)
  }
})
$('.file3').on('change',function(e){
	console.log($(this).index)
	var e=e||event;
	 var reader = new FileReader();
	 console.log(reader)
	reader.readAsDataURL($('.file3')[0].files[0]);//发起异步请求
	//上传图片4
	sendImg('.tu3 img', $('.file3')[0].files[0])
	reader.onload = function(){
    var src=this.result
	$('.tu3 img').attr('src',src)
  }
})
$('.xuanze').on('click',function(){
	xg=true
})
function sendImg(obj,msrc){
	var formData = new FormData();
	formData.append('file',msrc);
	$.ajax({
		url: baseNews + '/oss/upload/img',
		data: formData,
		type: "post",
		cache: false,
		processData: false,
		contentType: false,
		success: function (res) {
			console.log(res);
			if(res.code==0){
				$(obj).attr("fsrc", res.data);
			}else{
				zeroModal.alert('上传失败');
				return false;
			}
		}

	})
}
$(function (){
	//获取频道列表
	$.ajax({
		url: baseNews +"/api/data/special/channel/select",
		success:function (res){
			console.log(res);
			if(res.code==0){
				var obj=$(".ad_select");
				var obj1 = $(".pd_list");
				$(res.data).each(function (index,key){
					obj.append("<option value='"+key.id+"'>"+key.value+"</option>");
					obj1.append("<option value='" + key.id + "'>" + key.value + "</option>");
				})
			}
		}
	})
	//获取默认热度
	$.ajax({
		url: baseNews + "/api/data/special/inithot",
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				$(".rd_m").val(res.data);
				
			}
		}
	})
	// var json = {
	// 	"cname": "时政",
	// 	"csort": 20,
	// 	"id": 1,
	// 	"reveal": true
	// }
	// var result = JSON.stringify(json);
	// $.ajax({
	// 	url: baseNews + '/api/data/channel/base/add',
	// 	type: "post",
	// 	headers: { 'Content-Type': 'application/json' },
	// 	dataType: "json",
	// 	data: result,
	// 	success(res) {
	// 		console.log(res)
	// 	}
	// })
	//创建
	$(".btn_sure").on("click",function (){
		var t_name = $(".t_name").val()||"";
		var t_top = $(".t_top").prop("checked");
		var t_jianjie = $(".t_jianjie").val() || "";
		var t1img = $(".tu img").attr("fsrc");
		var app_f = $(".app_f").val();
		var t2img = $(".tu1 img").attr("fsrc");
		var t3img = $(".tu2 img").attr("fsrc");
		var t4img = $(".tu3 img").attr("fsrc");
		var secid = $(".ad_select").val();
		var inithot = $(".rd_m").val();
		var b_bianqian = $(".b_bianqian").attr("value");//标签
		var tags = "";
		// $(".input_box a").each(function (index, key) {
		// 	tags = tags + $(this).find("span").html() + ',';
		// })
		$(".tag").each(function (index, key) {
			var cont = $(this).find("span").html();
			for (var i = 0; i < bxlist.length; i++) {
				if (bxlist[i].tag == cont) {
					tags = tags + bxlist[i].id + ","
				}
			}

		})
		tags = tags.substring(0, tags.length - 1);
		console.log(t_top);
		if (inithot==""){
			zeroModal.alert('请填写初始热度');
			return false;
		}
		if (t_name == "") {
			zeroModal.alert('请输入专题名称');

			return false;
		}
		console.log(inithot,888);
		var json={
			"cid": secid,
			"cover": t1img,
			"img1": t2img,
			"img2": t3img,
			"img3": t4img,
			"inithot": inithot,
			"showtype": app_f,
			"synopsis": t_jianjie,
			"tags": tags,
			"title": t_name,
			"top": t_top
		}
		console.log(json)
		var result = JSON.stringify(json);
		zeroModal.loading(5);
		$.ajax({
			url: baseNews + "/api/data/special/add",
			type: "post",
	        headers: { 'Content-Type': 'application/json' },
			dataType: "json",
			data:result,
			success:function (res){
				console.log(res);
				if(res.code==0){	
					location.reload();
				}else{
					zeroModal.alert('创建失败');
					
				}
			}

		 })
	})
	//编辑默认获取列表
	//修改的提交
	//创建
	$(".btn_xiugai").on("click", function () {
		var t_name = $(".t_name").val() || "";
		var t_top = $(".t_top").prop("checked");
		var t_jianjie = $(".t_jianjie").val() || "";
		var t1img = $(".tu img").attr("fsrc");
		var app_f = $(".app_f").val();
		var t2img = $(".tu1 img").attr("fsrc");
		var t3img = $(".tu2 img").attr("fsrc");
		var t4img = $(".tu3 img").attr("fsrc");
		var secid = $(".ad_select").val();
		var inithot = $(".rd_m").val();
		var b_bianqian = $(".b_bianqian").attr("value");//标签
		var tags = "";
		// $(".input_box a").each(function (index, key) {
		// 	tags = tags + $(this).find("span").html() + ',';
		// })
		$(".tag").each(function (index,key){
			var cont=$(this).find("span").html();
			for (var i = 0; i < bxlist.length;i++){
				if (bxlist[i].tag == cont){
					tags = tags +bxlist[i].id+","
				}
			}

		})
		tags = tags.substring(0, tags.length - 1);
		console.log(tags, 3333333333);
		console.log(t_top);
		if (inithot == "") {
			zeroModal.alert('请填写初始热度');
			return false;
		}
		if (t_name == "") {
			zeroModal.alert('请输入专题名称');
			return false;
		}
		console.log(inithot, 888);
		var json = {
			"cid": secid,
			"id": $(".zdid").val(),
			"cover": t1img,
			"img1": t2img,
			"img2": t3img,
			"img3": t4img,
			"inithot": inithot,
			"showtype": app_f,
			"synopsis": t_jianjie,
			"tags": tags,
			"title": t_name,
			"top": t_top
		}
		console.log(json)
		var result = JSON.stringify(json);
		zeroModal.loading(5);
		$.ajax({
			url: baseNews + "/api/data/special/update",
			type: "post",
			headers: { 'Content-Type': 'application/json' },
			dataType: "json",
			data: result,
			success: function (res) {
				console.log(res);
				if (res.code == 0) {
					location.reload();
				} else {
					zeroModal.closeAll()
					zeroModal.alert(res.message);
				}
			}

		})
	})
})
//搜索
$(function (){
	
	$(".zt_state").change(function () {
		jsonList.state= $(this).val();
	})
	$(".pd_list").change(function () {
		jsonList.cid = $(this).val();
	})
	$(".btn_sqs").on('click', function () {
		var ztid = $(".ztid").val() || "";//专题id
		var zimc = $(".zimc").val() || "";//专题名称
		if (ztid != "") {
			jsonList.id = ztid
		}
		if (zimc != "") {
			jsonList.title = zimc
		}
		page = 1;
		getList();
	})
})
//获取默认列表
getList();
function getList() {
	var result = JSON.stringify(jsonList);
	$.ajax({
		url: baseNews +"/api/data/special/list?page="+page+"&rows="+rows,
		type: "post",
		headers: { 'Content-Type': 'application/json' },
		dataType: "json",
		data: result,
		success:function (res){
			console.log(res,666);
			if(res.code==0){
				var result=res.data;
				var list=result.list;
				var html = "";
				$("#page").paging({
					pageNo: page,
					totalPage: result.totalpage,
					totalSize: result.totals,
					callback: function (num) {
						page=num;
						getList();
					}
				})
				$(".tab_content").empty();
				$(list).each(function (index,key){
					console.log(index);
					var state=key.state;
					var newState="";
					if(state==1){
						newState="待审核";		
					}
					if (state == 2) {
						newState="待发布";
					}
					if (state == 3) {
						newState="已发布";
					}
					if (state == 10) {
						newState="有更新";
					}
					if (state == -1) {
						newState="审核失败";
					}
					if (state == -2) {
						newState="撤销发布";
					}
					html +=`<div class="div">
					<div class="b-t-s ww bor b-s-active">
						<input type="checkbox" oid="${key.id}" name='sex' class="xuanze">
					</div>
					<div class="b-t-s ww b-s-active">${key.id}</div>
					<div class="b-t-s ww b-s-active">${key.title?key.title:''}</div>
					<div class="b-t-s ww b-s-active">${newState?newState:''}</div>
					<div class="b-t-s ww bor b-s-active">${key.cname?key.cname:''}</div>
					<div class="b-t-s ww b-s-active"><span class="arc_open" data-title="${key.title}" arid="${key.id}" style="cursor: pointer;color:#0073A9;display:inline-block;text-align:center">查看文章</span></div>
					<div class="b-t-s ww b-s-active">${key.top==true?"是":"否"}</div>
					<div class="b-t-s ww b-s-active">${key.click?key.click:""}</div>
					<div class="b-t-s ww b-s-active">${key.collect?key.collect:''}</div>
					<div class="b-t-s ww b-s-active">${key.follow?key.follow:''}</div>
					<div class="b-t-s ww b-s-active">${key.pass?key.pass:''}</div>
					<div class="b-t-s ww b-s-active">${key.comment?key.comment:""}</div>
					<div class="b-t-s ww b-s-active">${key.heating?key.heating:''}</div>
					<div class="b-t-s ww b-s-active">${key.cooling?key.cooling:""}</div>
					<div class="b-t-s ww b-s-active">${key.hot?key.hot:''}</div>
					<div class="b-t-s ww b-s-active">${key.username?key.username:""}</div>
					<div class="b-t-s ww b-s-active">${key.createtime?key.createtime:""}</div>
					<div class="b-t-s ww b-s-active">${key.updatetime?key.updatetime:''}</div>
					<div class="b-t-s ww b-s-active">${key.atime?key.atime:""}</div>
					<div class="b-t-s ww b-s-active">${key.rtime?key.rtime:''}</div>
					</div>`;
				})
				$(".tab_content").append(html);
			}
		}
		
	})
	//分页
	
}
$(function (){
	//获取文章列表详情
	$(document).on("click", ".arc_open", function () {
		var id = $(this).attr("arid");
		console.log(id);
		window.location.href = "./fabu_wz.html?id=" + id;
		//var id =  $(this).attr("data-id");
        // var name = $(this).attr("data-title");
        // localStorage.setItem("zt_name",name);
        // window.location.href = "./ztgx_ck.html?ztid=" + id;
	})
	//获取专题列表

})

$(function (){
	//获取专题的标签详情
	$.ajax({
		url: baseNews + "/api/data/special/tag/list",
		success:function (res){
			console.log(res,666);
			var result=res.data;
			var arr=[];
			bxlist = result;
			arr = result.map(function (key,index){
				return `${key.tag}`;
			})
			zt_biao = arr;

			
		}
	})
})